// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var Action = flame.Action;

//////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////

// For use with throwsError.
function makeActionFactory(name) {
  return function() { return new Action(name); };
}

function ActionTest() {}
registerTestSuite(ActionTest);

//////////////////////////////////////////////////////
// Tests
//////////////////////////////////////////////////////

ActionTest.prototype.NoColonInName = function() {
  expectThat(
      makeActionFactory('foo/bar/baz'),
      throwsError(/Invalid action name.*foo\/bar\/baz/));
};

ActionTest.prototype.MultipleColonsInName = function() {
  expectThat(
      makeActionFactory('foo:bar:baz'),
      throwsError(/Invalid action name.*foo:bar:baz/));
};

ActionTest.prototype.NonAsciiInPackagePart = function() {
  expectThat(
      makeActionFactory('foo/김치:bar'),
      throwsError(/Invalid action name.*foo\/김치:bar/));
};

ActionTest.prototype.NonAsciiInNamePart = function() {
  expectThat(
      makeActionFactory('foo/bar:김치'),
      throwsError(/Invalid action name.*foo\/bar:김치/));
};

ActionTest.prototype.LeadingDigitInNamePart = function() {
  expectThat(
      makeActionFactory('foo/bar:9foo'),
      throwsError(/Invalid action name.*foo\/bar:9foo/));
};

ActionTest.prototype.NamePartEmpty = function() {
  expectThat(
      makeActionFactory('foo/bar:'),
      throwsError(/Invalid action name.*foo\/bar:/));
};

ActionTest.prototype.PackagePartEmpty = function() {
  var action = new Action(':foo');
  expectEq(':foo', action.name);
};

ActionTest.prototype.LegalNames = function() {
  expectEq('foo/bar:baz_9', new Action('foo/bar:baz_9').name);
  expectEq('foo/bar:_Baz_9', new Action('foo/bar:_Baz_9').name);
};

ActionTest.prototype.DefaultValues = function() {
  var action = new Action('foo/bar:baz');

  expectThat(action.inputs, elementsAre([]));
  expectThat(action.outputs, elementsAre([]));
  expectThat(action.commands, elementsAre([]));
  expectThat(action.userInfo, recursivelyEquals({}));
};
